package org.soarsky.wealthchengdu.module.info;

import java.util.ArrayList;
import java.util.List;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.entity.JsonResult;
import org.soarsky.wealthchengdu.entity.LogonResult;
import org.soarsky.wealthchengdu.entity.NewsContentData;
import org.soarsky.wealthchengdu.entity.Pic;
import org.soarsky.wealthchengdu.network.ParserResults;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 成都镜像大图
 * 
 * @author 胡伟 下午3:12:29
 */
public class MirrorBigImageActivity extends Activity {

	Gallery delitGallery;
	MirrordelitAdapter mAdapter;
	ProgressBar loaddatagress;
	ParserResults service;

	private TextView backtv;
	private TextView titletv;
	private TextView drictetv;
	private LinearLayout delitlinearlayout;

	List<Pic> listpic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_imgs_delit);
		service = new ParserResults();
		listpic = new ArrayList<Pic>();
		initview();
		setviewdata();
		loadData();
		setclick();
	}

	private void initview() {
		delitGallery = (Gallery) findViewById(R.id.activity_delit_gallery);
		delitlinearlayout = (LinearLayout) findViewById(R.id.activity_delit_linearlayout);
		backtv = (TextView) findViewById(R.id.activity_galler_back);
		titletv = (TextView) findViewById(R.id.activity_galler_title);
		drictetv = (TextView) findViewById(R.id.activity_galler_direction);
	}

	private void setviewdata() {

		mAdapter = new MirrordelitAdapter(MirrorBigImageActivity.this);
		delitGallery.setAdapter(mAdapter);
		titletv.setText(MyAppliction.galleryname);
	}

	private void loadData() {
		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {
				// TODO Auto-generated method stub
				getLogon();
				try {
					JsonResult<NewsContentData> result = service
							.getNewsContent(MyAppliction.ID);
					if (result == null)
						return false;
					if (result.isError()) {
						error(result);
						// 错误处理
						return false;
					} else {
						NewsContentData dataMsg = result.getData();
						if (dataMsg != null) {// 判断是否有数据
							for (Pic pic : dataMsg.getPic()) {
								listpic.add(pic);
							}
							return true;
						}

					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
				}
				return false;
			}

			protected void onPostExecute(Boolean result) {
				// UI更新操作
				if (result) {
					mAdapter.setTempList(listpic);
					setdirect(0);
				} else {

				}

			};
		}.execute();
	}

	// 必须先登录
	private void getLogon() {
		try {
			JsonResult<LogonResult> jsonResult = service.authLogon("张三");
			if (jsonResult != null) {
				if (jsonResult.isError()) {
					error(jsonResult);
				} else {// 登录成功
					System.out.println(jsonResult.getData().getUserId() + "==="
							+ jsonResult.getData().getNickName());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.toString() + "===");
		}
	}

	private static void error(JsonResult<?> jsonResult) {
		System.out.println("错误码:" + jsonResult.getCode() + "===错误信息:"
				+ jsonResult.getMsg());
	}

	public void setclick() {
		delitGallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (delitlinearlayout.isShown()) {
					delitlinearlayout.setVisibility(View.INVISIBLE);
				} else {
					delitlinearlayout.setVisibility(View.VISIBLE);
				}

			}
		});

		backtv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MirrorBigImageActivity.this.finish();

			}
		});
	}

	public void setdirect(int position) {

		delitGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (MyAppliction.mCn == 0) {
					drictetv.setText(listpic.get(arg2).getDesc());
				} else {
					drictetv.setText(listpic.get(arg2).getDescEn());
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		if (MyAppliction.mCn == 0) {
			drictetv.setText(listpic.get(position).getDesc());
		} else {
			drictetv.setText(listpic.get(position).getDescEn());
		}
	}
}
